package cn.dapeng.utils;

import cn.dapeng.linked.DoubleLinkedNode;
import cn.dapeng.linked.LinkedNode;

/**
 * @BelongProject study
 * @BelongPackage cn.dapeng.linked
 * @Description TODO
 * @ClassName LinkedCreator
 * @Author xupenghong
 * @Date 2024/8/20 13:34
 * @Version 1.0.0
 */
public class LinkedCreator {

    public static LinkedNode createLinked(int maxValue, int maxLength) {
        int length = (int) (Math.random() * maxLength);
        while (length < 3) {
            length = (int) (Math.random() * maxLength);
        }
        LinkedNode head = new LinkedNode((int) (Math.random() * maxValue));
        LinkedNode pre = head;
        for (int i = 1; i < length; i++) {
            pre.next = new LinkedNode((int) (Math.random() * maxValue));
            pre = pre.next;
        }

        return head;
    }

    public static LinkedNode createLinked(int[] array) {
        LinkedNode head = new LinkedNode();

        LinkedNode pre = head;
        head.value = array[0];
        for (int i = 1; i < array.length; i++) {
            pre.next = new LinkedNode(array[i]);
            pre = pre.next;
        }

        return head;
    }

    /**
     * 创建一个升序的链表
     *
     * @return
     */
    public static LinkedNode createAscLinked(int maxValue, int maxLength) {

        int length = 0;
        while (length < 3) {
            length = (int) (Math.random() * maxLength);
        }

        int value = (int) (Math.random() * maxValue);
        int nextNodeValue;
        LinkedNode head = new LinkedNode(value), pre = head;
        for (int i = 1; i < length; i++) {
            do {
                nextNodeValue = (int) (Math.random() * maxValue);
            } while (nextNodeValue < pre.value);

            pre.next = new LinkedNode(nextNodeValue);
            pre = pre.next;

        }

        return head;
    }

    public static void print(LinkedNode head) {
        LinkedNode cur = head;
        while (cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public static int getLength(LinkedNode head) {
        int length = 0;
        while (head != null) {
            length++;
            head = head.next;
        }
        return length;
    }

    /**
     * 判断链表是否是升序
     *
     * @param head
     * @return
     */
    public static boolean checkIfAsc(LinkedNode head) {
        while (head != null && head.next != null) {
            if (head.value > head.next.value) {
                return false;
            } else {
                head = head.next;
            }
        }
        return true;
    }


    /* ********************************************************************** */

    /**
     * 创建一个双端链表
     *
     * @param maxValue
     * @param maxLength
     * @return
     */
    public static DoubleLinkedNode createDoubleLinkedNode(int maxValue, int maxLength) {

        int length = 0;

        while (length == 0) {
            length = (int) (Math.random() * maxLength);
        }

        DoubleLinkedNode head = new DoubleLinkedNode((int) (Math.random() * maxValue)), next = head, pre;
        for (int i = 1; i < length; i++) {
            pre = next;
            next.next = new DoubleLinkedNode((int) (Math.random() * maxValue));
            next = next.next;
            next.pre = pre;
        }
        return head;
    }

    public static int getDoubleLinkedNodeLength(DoubleLinkedNode head) {
        int length = 0;

        DoubleLinkedNode next = head;
        while (next != null) {
            length++;
            next = next.next;
        }

        return length;
    }

    public static void print2(DoubleLinkedNode head) {
        while (head != null) {
            System.out.print(head.value + " ");
            head = head.next;
        }
        System.out.println();
    }


}
